package com.zjb.home.boxingtu.view.other;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

/**
 * Created by zhangjiebo on 2019/10/24.
 * 线条指示器
 *
 * @author ZhangJieBo
 */
public class LineIndicatorShakeView extends View {

    private Paint paint;
    private int num = 3;
    private float offset;
    private int position = 0;
    private boolean isShake = true;
    private float shakeValue = 0f;
    private float range = 0.2f;
    private long time = 1500L;

    public LineIndicatorShakeView(Context context) {
        super(context);
        init();
    }

    public LineIndicatorShakeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LineIndicatorShakeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.RED);
        paint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setStrokeWidth(getHeight());
        float oneLineWidth = (getWidth() - getHeight()) / (float) num;
        float shakedistance = isShake?oneLineWidth*shakeValue*range:0;
        canvas.drawLine(oneLineWidth * position + getHeight() / 2f + offset +shakedistance, getHeight() / 2f, oneLineWidth * (position + 1f) + getHeight() / 2f + offset+shakedistance, getHeight() / 2f, paint);
    }

    public void bindViewPager(ViewPager viewPager) {
        num = viewPager.getAdapter().getCount();
        invalidate();


        ObjectAnimator animator1 = ObjectAnimator.ofFloat(this,"shakeValue",0f,1f);
        animator1.setDuration(time);
        animator1.setInterpolator(new LinearInterpolator());
        animator1.setRepeatCount(ValueAnimator.INFINITE);
        animator1.setRepeatMode(ValueAnimator.REVERSE);
        animator1.start();

        ObjectAnimator animator2 = ObjectAnimator.ofFloat(this,"shakeValue",0f,1f,0f,-1f,0f);
        animator2.setDuration(time*3);
        animator2.setInterpolator(new LinearInterpolator());
        animator2.setRepeatCount(ValueAnimator.INFINITE);
        animator2.setRepeatMode(ValueAnimator.RESTART);
//        animator2.start();

        ObjectAnimator animator3 = ObjectAnimator.ofFloat(this,"shakeValue",0f,-1f);
        animator3.setDuration(time);
        animator3.setInterpolator(new LinearInterpolator());
        animator3.setRepeatCount(ValueAnimator.INFINITE);
        animator3.setRepeatMode(ValueAnimator.REVERSE);
//        animator3.start();


        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                LineIndicatorShakeView.this.position = position;
                offset = (getWidth() - getHeight()) / (float) num * positionOffset;
                if (position==0){
                    animator1.start();
                    animator2.end();
                    animator3.end();
                }else if (position==num-1){
                    animator1.end();
                    animator2.end();
                    animator3.start();
                }else {
                    animator1.end();
                    animator2.start();
                    animator3.end();
                }
                if (positionOffset == 0) {
                    isShake = true;
                } else {
                    isShake = false;
                }
                invalidate();
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setShakeValue(float shakeValue) {
        this.shakeValue = shakeValue;
        invalidate();
    }
}
